import requests
import json
import logging
import re
from Scripts.headers import feishu_header, config, log_format


logging.basicConfig(level=logging.WARNING, format=log_format)


def search_user(search_user_id):
    # 请求地址###
    url = f"https://open.feishu.cn/open-apis/contact/v3/users/{search_user_id}?user_id_type=user_id"
    result = requests.get(url, headers=feishu_header())
    user = {}
    user_json = json.loads(result.text)["data"]["user"]
    try:
        user['name_pinyin'] = user_json["enterprise_email"].split('@')[0]
        user['en_name'] = re.findall(r'(.*?)[( |（]', user_json["name"])[0]
        user['DisplayName'] = re.findall(r'[(|（](.*?)[)|）]', user_json["name"])[0]
        user['emailAddress'] = user_json["enterprise_email"]
    except Exception as e:
        logging.warning(f'{e}')
        return False

    # 当输入用户为测试用户时，自动更改为测试###
    if user['name_pinyin'] == config.get("test", 'user_name'):
        user['name_pinyin'] = "test"
        user['en_name'] = '测试'
        user['emailAddress'] = 'test@rouzao.com'
    return user

